﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class admin_cms_Dev_advertis_banner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            load_drop2();
            load_linkpage();
            // load dữ liệu lên griv
            Griv_Banner_all();
        }
    }
    /// <summary>
    /// Thêm mới một quảng cáo
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void bt_them_advertis_banner(object sender, EventArgs e)
    {
        string title = txttile.Text;
        string hyperlink = txtredirect.Text;
        string link_web = drMenu.SelectedItem.Value.ToString();
        string link_page = DrlLinkPage.SelectedItem.Value.ToString();
        string filename = "";
        bool isimage=false;
        bool isflash=false;
        string createby = " ";
        if (Session["timeout"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            createby = Session["timeout"].ToString();
        }
        if (fulImage_Qc.HasFile)
        {
            if ( Path.GetExtension(fulImage_Qc.FileName) == ".swf")
            {
                filename = fulImage_Qc.FileName;
                fulImage_Qc.SaveAs(Server.MapPath("~/files_upload/advertis/flash/" + filename));
               
                isflash = true;
            }
            else
            {
                filename = fulImage_Qc.FileName;
                fulImage_Qc.SaveAs(Server.MapPath("~/files_upload/advertis/images/" + filename));
                isflash = false;
                isimage = true;
            }
            DataAccess.ExeNonStored("dev_advertis_banner_Insert", "@title", title, "@hyperlink", hyperlink, "@link_web", link_web, "@link_page", link_page, "@file_name", filename, "@isimage", isimage, "@isflash", isflash, "@createby", createby);
            Griv_Banner_all();
        }


        
        
    }

    protected void Griv_Banner_all()
    {
        DataTable dt_grive_banner = new DataTable();
        dt_grive_banner = DataAccess.ExeStoredToDataTable("dev_advertis_banner_LoadAll");
        Griv_Banner.DataSource = dt_grive_banner;
        Griv_Banner.DataBind();
    }
    protected void bt_sua_advertis_banner(object sender, EventArgs e)
    {
        string title = txttile.Text;
        string hyperlink = txtredirect.Text;
        string link_web = drMenu.SelectedItem.Value.ToString();
        string link_page = DrlLinkPage.SelectedItem.Value.ToString();
        string filename = "";
        bool isimage;
        bool isflash;
        string updateby = " ";
        if (Session["timeout"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            updateby = Session["timeout"].ToString();
        }
        if (fulImage_Qc.HasFile)
        {
            if (fulImage_Qc.FileName.ToString() == ".swf")
            {
                filename = fulImage_Qc.FileName;
                fulImage_Qc.SaveAs(Server.MapPath("~/files_upload/advertis/flash/" + filename));
                isimage = false;
                isflash = true;
            }
            else
            {
                filename = fulImage_Qc.FileName;
                fulImage_Qc.SaveAs(Server.MapPath("~/files_upload/advertis/images/" + filename));
                isflash = false;
                isimage = true;
            }
            if (Session["id_banner"] != null)
            {
                int id = Convert.ToInt32(Session["id_banner"]);
                DataAccess.ExeNonStored("dev_advertis_banner_update","@id",id, "@title", title, "@hyperlink", hyperlink, "@link_web", link_web, "@link_page", link_page, "@file_name", filename, "@isimage", isimage, "@isflash", isflash, "@updateby", updateby);
                Griv_Banner_all();
            }

        }


    }
    protected void load_linkpage()
    {
        DataTable dt_linkpage = new DataTable();
        dt_linkpage = DataAccess.ExeStoredToDataTable("dev_page_link_all");
        if (dt_linkpage.Rows.Count > 0)
        {
            DrlLinkPage.DataSource = dt_linkpage;
            DrlLinkPage.DataTextField = "data_name";
            DrlLinkPage.DataValueField = "link_page";
            DrlLinkPage.DataBind();
        }
    }
    protected void load_drop2()
    {
        DataTable dtDrop = new DataTable();
        dtDrop.Columns.Add("name_menu", System.Type.GetType("System.String"));
        dtDrop.Columns.Add("link_web", System.Type.GetType("System.String"));

        DataTable dtDatadrop = DataAccess.ExeSQLToDataTable("SELECT * FROM dev_Menu");

        load_drop_process(dtDrop, dtDatadrop, "0", 0);

        drMenu.DataTextField = "name_menu";
        drMenu.DataValueField = "link_web";
        drMenu.DataSource = dtDrop;
        drMenu.DataBind();
    }
    /// <summary>
    /// load theo đệ quy vào dropdownlist
    /// </summary>
    /// <param name="dtDrop"></param>
    /// <param name="dtnews"></param>
    /// <param name="IdParent"></param>
    /// <param name="level"></param>
    /// <returns></returns>
    protected DataTable load_drop_process(DataTable dtDrop, DataTable dtnews, string IdParent, int level)
    {
        int countall = dtnews.Rows.Count;

        for (int i = 0; i < countall; i++)
        {
            if (dtnews.Rows[i][1].ToString() == IdParent)
            {
                string idMenu = dtnews.Rows[i][0].ToString();

                //Khai 1 bao doi tuong DataRow
                DataRow drItems;
                drItems = dtDrop.NewRow();

                drItems["name_menu"] = levelMenu(level) + dtnews.Rows[i][2].ToString();
                drItems["link_web"] = dtnews.Rows[i][3].ToString();
                dtDrop.Rows.Add(drItems);

                load_drop_process(dtDrop, dtnews, idMenu, level + 1);
            }
        }
        return dtDrop;
    }

    protected string levelMenu(int value)
    {
        string str = "";
        for (int i = 0; i < value; i++)
        {
            str += "*";
        }
        return str;
    }


    public static string test_browrs_menu(bool value)
    {
        string htmlCode = "menu_browrs_off";
        if (value == true)
        {
            htmlCode = "menu_browrs_on";
        }
        return htmlCode;
    }

    public static string test_cant_delete(bool value)
    {
        string htmlCode = "<img alt='' src='style/img/close_16.png' title='Click to Delete' />";
        if (value == true)
        {
            htmlCode = "<img alt='' src='style/img/close_off_16.png' title='Can not Delete' />";
        }

        return htmlCode;
    }

    public static bool bool_inverse(bool value)
    {
        bool set = false;
        if (value == false)
        {
            set = true;
        }
        return set;
    }
    protected void news_grAll_click(object sender, CommandEventArgs e)
    {
        string Argvalue = e.CommandArgument.ToString();
        char[] ch = new char[] { '|' };

        string[] s = ITNLip.SplitString(Argvalue, ch);
        string id = s[0];
        string action = s[1];
        string value = s[2];

        switch (action)
        {
            case "top":
                string istop = "0";
                if (value == "False")
                {
                    istop = "1";
                }
                DataAccess.ExeNonStored("dev_advertis_banner_update_istop", "@id", id, "@istop", istop);
                break;
            case "hot":
                string ishot = "0";
                if (value == "False")
                {
                    ishot = "1";
                }
                DataAccess.ExeNonStored("dev_advertis_banner_update_ishot", "@id", id, "@ishot", ishot);
                break;
            case "browrs":
                string status = "0";
                if (value == "False")
                {
                    status = "1";
                }
                DataAccess.ExeNonStored("dev_advertis_banner_update_status", "@id", id, "@status", status);
                break;
            case "delete":

                DataAccess.ExeNonStored("dev_advertis_banner_delete", "@id", id);
                break;
            case "edit":
                DataTable dt_admin_advertis = new DataTable();
                dt_admin_advertis = DataAccess.ExeStoredToDataTable("dev_advertis_banner_Load_Select", "@id", id);
                if (dt_admin_advertis.Rows.Count > 0)
                {
                    txttile.Text = dt_admin_advertis.Rows[0][1].ToString();
                    txtredirect.Text = dt_admin_advertis.Rows[0][2].ToString();
                    Session.Add("id_banner", id);
                }
                break;
        }
        Griv_Banner_all();
    }
}
